package com.grgbanking.encryp.web;


import com.grgbanking.encryp.bean.KeyInfo;
import com.grgbanking.encryp.service.KeyInfoService;
import com.grgbanking.encryp.service.KeyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class ApiController {

    @Autowired
    private KeyInfoService keyinfoService;

    @GetMapping("/init")
    public String init(){
        KeyInfo keyinfo = null;
        for(int i=0;i<10;i++){
            keyinfo = new KeyInfo();
            keyinfo.setName("test"+i);
            keyinfoService.save(keyinfo);
        }
        return "初始化完成。";
    }

    @GetMapping("/userByName/{username}")
    public KeyInfo getUserByName(@PathVariable("username") String username){
        return keyinfoService.getByName(username);
    }

    @GetMapping("/userById/{userid}")
    public KeyInfo getUserById(@PathVariable("userid") Long userid){
        return keyinfoService.getUserByID(userid);
    }

    @GetMapping("/page")
    public Page<KeyInfo> getPage(){
        return keyinfoService.findPage();
    }

    @GetMapping("/page/{maxID}")
    public Page<KeyInfo> getPageByMaxID(@PathVariable("maxID") Long maxID){
        return keyinfoService.find(maxID);
    }

    @RequestMapping("/update/{id}/{name}")
    public KeyInfo update(@PathVariable Long id, @PathVariable String name){
        return keyinfoService.update(id,name);
    }

}